using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;

namespace RedPointSystem
{
    /// <summary>
    /// 提示红点
    /// </summary>
    public class RedPoint : MonoBehaviour
    {
        [SerializeField, HideInInspector]
        public List<RedPointData> m_RedPointList = null;

        public Image m_icon = null;
        
        private RedPointData redPointData = null;

        public RedPointData RedPointData
        {
            get { return redPointData; }
        }

        private void OnEnable()
        {
            foreach (var rp in m_RedPointList)
            {
                RedPointCheckMgr.GetInstance().AddRedPoint(rp.redPointTypeId, this);
            }
            
            Check();
        }

        private void OnDisable()
        {
            foreach (var rp in m_RedPointList)
            {
                RedPointCheckMgr.GetInstance().RemoveRedPoint(rp.redPointTypeId, this);
            }
        }

        private void OnDestroy()
        {
            foreach (var rp in m_RedPointList)
            {
                RedPointCheckMgr.GetInstance().RemoveRedPoint(rp.redPointTypeId, this);
            }
        }

        public void Check()
        {
            RedPointCheckResultData resultData = null;
            bool isExist = false;
            if (m_RedPointList == null)
            {
                return;
            }

            redPointData = m_RedPointList.Count > 0 ? m_RedPointList[0] : null;
            for (int i = 0; i < m_RedPointList.Count; i++)
            {
                resultData = RedPointCheckMgr.GetInstance().Check(this, m_RedPointList[i]);
                if (resultData.isExsit)
                {
                    redPointData = m_RedPointList[i];
                    isExist = true;
                    break;
                }
            }

            DrawIcon(isExist);
        }

        public void DrawIcon(bool isExist, bool isEditor = false, RedPointData rd = null)
        {
            if (isEditor)
            {
                this.redPointData = rd;
                Transform t = transform.Find("redpoint_icon");
                if (t != null)
                {
                    m_icon = t.GetComponent<Image>();
                }

                if (m_icon == null && t != null)
                {
                    GameObject.DestroyImmediate(t.gameObject);
                }
            }

            if (!isExist)
            {
                if (m_icon != null)
                {
                    if (isEditor)
                    {
                        GameObject.DestroyImmediate(m_icon.gameObject);
                        m_icon = null;
                    }
                    else
                    {
                        m_icon.gameObject.SetActive(false);
                    }
                }

                return;
            }

            if (m_icon == null)
            {
                GameObject g = new GameObject("redpoint_icon");
                g.transform.SetParent(transform);
                g.transform.localScale = Vector3.one;
                g.transform.localPosition = Vector3.zero;
                g.transform.localEulerAngles = Vector3.zero;
                m_icon = g.AddComponent<Image>();
            }

            if (null != m_icon)
            {
                if (isEditor && !Application.isPlaying)
                {
#if UNITY_EDITOR
                    string p = "Assets/Resources/Atlas_RedPoints/redPoint" + (int) redPointData.style + ".png";
                    m_icon.sprite = UnityEditor.AssetDatabase.LoadAssetAtPath<Sprite>(p);
#endif
                }
                else
                {
                    m_icon.sprite = Resources.Load<Sprite>("Atlas_RedPoints/redPoint" + (int) redPointData.style);
                }

                m_icon.SetNativeSize();
            }

            if (m_icon.raycastTarget)
            {
                m_icon.raycastTarget = false;
            }

            m_icon.transform.localScale = Vector3.one;
            if (!m_icon.gameObject.activeInHierarchy)
            {
                m_icon.gameObject.SetActive(true);
            }

            m_icon.transform.localPosition = redPointData.localPos;
        }

        public void SetConditionByTypeId(int typeId, int condition1 = -1, int condition2 = -1)
        {
            bool isFind = false;
            for (int i = 0; i < m_RedPointList.Count; ++i)
            {
                if (typeId == m_RedPointList[i].redPointTypeId)
                {
                    m_RedPointList[i].remindContion = condition1;
                    m_RedPointList[i].remindContion2 = condition2;
                    isFind = true;
                    break;
                }
            }

            if (!isFind)
            {
                Debug.LogError($"未找到对应id检测条件，item = {name}, typeId = {typeId}");
            }
        }
    }
}
